<script setup lang="ts">
import useDictionaryStore from '@/stores/dictionary'
import { computed, onMounted } from 'vue'

const dictionaryStore = useDictionaryStore()

const {
  defaultLabel = '',
  parentDictionaryValue,
  value,
} = defineProps<{
  parentDictionaryValue: string
  value: string | undefined
  defaultLabel?: string
}>()

const labelComputed = computed(() => {
  const dictionary = dictionaryStore.map.get(parentDictionaryValue)
  let label = defaultLabel
  if (dictionary && value) {
    for (const item of dictionary) {
      if (item.dictionaryValue === value) {
        label = item.dictionaryCode
        break
      }
    }
  }
  return label
})

onMounted(async () => {
  await dictionaryStore.getDictionary(parentDictionaryValue)
})
</script>

<template>{{ labelComputed }}</template>
